package br.com.sisgerenciamento.dao.cliente;

import java.util.ArrayList;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.AliasToBeanResultTransformer;

import br.com.sisgerenciamento.exceptions.AlterarException;
import br.com.sisgerenciamento.exceptions.ApplicationException;
import br.com.sisgerenciamento.exceptions.ExcluirException;
import br.com.sisgerenciamento.exceptions.InserirException;
import br.com.sisgerenciamento.generic.GenericDAO;
import br.com.sisgerenciamento.model.cliente.ClienteVO;

public class ClienteDAOServiceImpl extends GenericDAO<ClienteVO> implements
		ClienteDAOService {

	public ClienteVO inserir(ClienteVO vo) throws InserirException {
		return (ClienteVO) insert(vo);
	}

	public void alterar(ClienteVO vo) throws AlterarException {
		update(vo);
	}

	public void excluir(ClienteVO vo) throws ExcluirException {
		delete(vo);
	}

	public ClienteVO consultarPorCodigo(Long codigo)
			throws ApplicationException {
		Session session = null;
		try {
			session = getSessionAberta();
			Criteria criteria = session.createCriteria(ClienteVO.class);
			criteria.add(Restrictions.eq("codigo", codigo));
			ClienteVO vo = (ClienteVO) consultar(criteria);
			return vo;
		} catch (Exception e) {
			throw new ApplicationException(e.getMessage());
		} finally {
			if (session != null)
				session.close();
		}
	}
	
	public ClienteVO consultarPorNomeExato(String nomeCliente)
			throws ApplicationException {
		Session session = null;
		try {
			session = getSessionAberta();
			Criteria criteria = session.createCriteria(ClienteVO.class);
			criteria.add(Restrictions.ilike("nome", nomeCliente));
			ClienteVO vo = (ClienteVO) consultar(criteria);
			return vo;
		} catch (Exception e) {
			throw new ApplicationException(e.getMessage());
		} finally {
			if (session != null)
				session.close();
		}
	}

	@SuppressWarnings("unchecked")
	public ArrayList<String> consultarPorNome(String nome)
			throws ApplicationException {
		Session session = null;
		try {
			session = getSessionAberta();
			Criteria criteria = session.createCriteria(ClienteVO.class);
			ProjectionList proList = Projections.projectionList();
			proList.add(Projections.property("nome"));
			criteria.setProjection(proList);
			criteria.add(Restrictions.ilike("nome", "%" + nome + "%"));
			ArrayList<String> list = (ArrayList<String>) criteria.list();
			return list;
		} catch (Exception e) {
			throw new ApplicationException(e.getMessage());
		} finally {
			if (session != null)
				session.close();
		}
	}

	public ArrayList<ClienteVO> consultarClientePorNome(String nome)
			throws ApplicationException {
		Session session = null;
		try {
			session = getSessionAberta();
			Criteria criteria = session.createCriteria(ClienteVO.class);
			criteria.add(Restrictions.ilike("nome", "%" + nome.trim() + "%"));
			ArrayList<ClienteVO> vos = (ArrayList<ClienteVO>) filtro(criteria);
			return vos;
		} catch (Exception e) {
			throw new ApplicationException(e.getMessage());
		} finally {
			if (session != null)
				session.close();
		}
	}

	@SuppressWarnings("unchecked")
	public ArrayList<ClienteVO> listarAtivos() throws ApplicationException {
		Session session = null;
		try {
			session = getSessionAberta();
			Query query = session.createQuery("SELECT c.nome as nome, c.codigo as codigo FROM ClienteVO c WHERE c.status = true order by c.nome");
			query.setResultTransformer(new AliasToBeanResultTransformer(ClienteVO.class));
			return (ArrayList<ClienteVO>) query.list();
		} catch (Exception e) {
			throw new ApplicationException(e.getMessage());
		} finally {
			if (session != null)
				session.close();
		}
	}

}
